import {Injectable} from '@angular/core';
import {FacilityMaintenanceRecord} from '../types/resp/FacilityMaintenanceRecord';
import {FacilityMaintenanceRecordForm} from '../types/req/FacilityMaintenanceRecordForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {FacilityMaintenanceRecordQueryForm} from '../types/req/FacilityMaintenanceRecordQueryForm';

@Injectable({
  providedIn: 'root'
})
export class FacilityMaintenanceRecordService {
  facilityMaintenanceRecords: FacilityMaintenanceRecord[] = [
    {id: 1, facilityId: 1, facilityName: '充电应急灯', maintenanceStartDate: '2024-02-02 08:30:00', maintenanceEndDate: '2024-02-02 18:00:00', staffId: 1, staffName: '赵六', remark: '一切正常', createTime: '2024-01-01 12:12:12'},
    {id: 2, facilityId: 2, facilityName: '高空摄像头', maintenanceStartDate: '2024-03-03 08:30:00', maintenanceEndDate: '2024-03-03 18:00:00', staffId: 2, staffName: '张三', remark: '一切正常', createTime: '2024-02-02 12:12:12'},
    {id: 3, facilityId: 3, facilityName: '顶楼换气扇', maintenanceStartDate: '2024-04-04 08:30:00', maintenanceEndDate: '2024-04-04 18:00:00', staffId: 3, staffName: '李四', remark: '一切正常', createTime: '2024-03-03 12:12:12'},
    {id: 4, facilityId: 4, facilityName: '大厅电风扇', maintenanceStartDate: '2024-05-05 08:30:00', maintenanceEndDate: '2024-05-05 18:00:00', staffId: 4, staffName: '王五', remark: '一切正常', createTime: '2024-04-04 12:12:12'}
  ]
  constructor() {
  }

  save (facilityMaintenanceRecordForm: FacilityMaintenanceRecordForm): Observable<Result<void>> {
    console.log('facilityMaintenanceRecordApi save params', facilityMaintenanceRecordForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (facilityMaintenanceRecordQueryForm: FacilityMaintenanceRecordQueryForm): Observable<Result<Page<FacilityMaintenanceRecord>>> {
    console.log('facilityMaintenanceRecordApi find params', facilityMaintenanceRecordQueryForm)
    return MockApi.queryPageSuccessfully(this.facilityMaintenanceRecords).pipe(
      map((response: Response<Result<Page<FacilityMaintenanceRecord>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<FacilityMaintenanceRecord>> {
    console.log('facilityMaintenanceRecordApi findById id', id)
    let target = {}
    for (const a of this.facilityMaintenanceRecords) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<FacilityMaintenanceRecord>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('facilityMaintenanceRecordApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
